package com.example.fan.airline_reservation.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.fan.airline_reservation.Sign_in;
import com.example.fan.airline_reservation.register;

public class GuidePagerAdapter extends PagerAdapter {
    private final int[] myImage;
    private final Context context;
    public GuidePagerAdapter(Context context, int[] myImage) {
        this.myImage=myImage;
        this.context=context;
    }

    @Override
    public int getCount() {
        return myImage.length;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView=new ImageView(context);
        LinearLayout.LayoutParams ivParams =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(ivParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(myImage[position]);
        if (position==myImage.length-1){
            imageView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    SharedPreferences usesetting=context.getSharedPreferences("printer",Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = usesetting.edit();
                    edit.putBoolean("first",false);
                    edit.commit();
                    Intent intent=new Intent(context, Sign_in.class);//换成要跳转的activity
                    context.startActivity(intent);
                    ((Activity)context).finish();
                }
            });
        }
        container.addView(imageView);
        return imageView;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {

        return view==o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

